package com.vtc.session4;

import java.util.ArrayList;
import java.util.List;

import com.vtc.session3.Employee;

import android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class ListView2Adapter extends BaseAdapter{

	private ArrayList<Employee> arr;
    private Context context;
	public ListView2Adapter(Context context){
		this.context = context;
		arr = new ArrayList<Employee>();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	@Override
	public Object getItem(int postion) {
		return arr.get(postion);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int postion, View view, ViewGroup parent) {
		final Employee e = arr.get(postion);
		if(view==null){
//			LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			TableLayout tl = (TableLayout) inflat.inflate(com.vtc.R.layout.listview_rowitem, null);
//			
//			TextView tvName =(TextView) tl.findViewById(com.vtc.R.id.txtName);
//			TextView tvGender=(TextView) tl.findViewById(com.vtc.R.id.txtGender);
//			TextView tvAge =(TextView) tl.findViewById(com.vtc.R.id.txtAge);
//			
//			Employee e = arr.get(postion);
//			tvName.setText(e.getName());
//			tvGender.setText(e.getGender());
//			tvAge.setText(e.getAge()+"");
			
			LayoutInflater inflat =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			LinearLayout tl = (LinearLayout) inflat.inflate(com.vtc.R.layout.listview2_rowitem2, null);
			ImageView img = (ImageView) tl.findViewById(com.vtc.R.id.ListView2_img);
			TextView  txtName = (TextView) tl.findViewById(com.vtc.R.id.listview2_txtFullname);
			TextView  txtGender= (TextView) tl.findViewById(com.vtc.R.id.listview2_txtGender);
			TextView  txtAge= (TextView) tl.findViewById(com.vtc.R.id.listview2_txtAge);
			CheckBox checkbox = (CheckBox) tl.findViewById(com.vtc.R.id.listview2_check);
			
			
			
			checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean flag) {				
					e.setChecked(flag);
				}
			});
			
			img.setImageResource(R.drawable.alert_dark_frame);
			checkbox.setChecked(e.isChecked());
			txtName.setText(e.getName());
			txtGender.setText(e.getGender());
			txtAge.setText(e.getAge()+"");
			
			return tl;
		}
		else{
			LinearLayout tl = (LinearLayout) view;
			ImageView img = (ImageView) tl.findViewById(com.vtc.R.id.ListView2_img);
			TextView  txtName = (TextView) tl.findViewById(com.vtc.R.id.listview2_txtFullname);
			TextView  txtGender= (TextView) tl.findViewById(com.vtc.R.id.listview2_txtGender);
			TextView  txtAge= (TextView) tl.findViewById(com.vtc.R.id.listview2_txtAge);
			CheckBox checkbox = (CheckBox) tl.findViewById(com.vtc.R.id.listview2_check);
			checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {				
				@Override
				public void onCheckedChanged(CompoundButton arg0, boolean flag) {				
					e.setChecked(flag);
				}
			});
			checkbox.setChecked(e.isChecked());
			img.setImageResource(R.drawable.alert_dark_frame);
			txtName.setText(e.getName());
			txtGender.setText(e.getGender());
			txtAge.setText(e.getAge()+"");
			return tl;
		}
		
	}
	public void add(Employee e){
		e.setImageId(arr.size());
		this.arr.add(e);
		notifyDataSetChanged();
	}
	public void update(Employee e){
		for(Employee e1:arr){
			if(e1.getImageId()==e.getImageId()){
				e1 = e;
				notifyDataSetChanged();
				break;
			}
		}
	}
	public void delete(){
		int count = arr.size();
		for(int i=count-1;i>=0;i--){
			Employee e = arr.get(i);
			if(e.isChecked()){
				arr.remove(i);
				System.out.println("Phin tu"+ e.getName());
			}
		}		
		arr.trimToSize();
		this.notifyDataSetChanged();
	}
}
